#include "Player.h"

#include "TileEngine.h"
#include "Layer.h"
#include "Tile.h"

#include "../SGD Wrappers/SGD_GraphicsManager.h"
#include "../SGD Wrappers/SGD_InputManager.h"


Player::Player(){

	//	Load Textures
	m_hImage = SGD::GraphicsManager::GetInstance()->LoadTexture(L"resource/graphics/char.png");

	//	Setup player data
	GetPosition() = SGD::Point(0.0f, 0.0f);
	m_SmellRadius = 224.0f;
	SetHealth(100.0f);
	SetStamina(100.0f);
}

Player::~Player(){
	SGD::GraphicsManager::GetInstance()->UnloadTexture(m_hImage);
}

void Player::Update(float elapsedTime, SGD::Size wsize, Layer* lay, SGD::Point offset){
	SGD::InputManager* input = SGD::InputManager::GetInstance();

	if (input->IsKeyDown(SGD::Key::Shift))
		m_moveSpeed = 2.0f;
	else
		m_moveSpeed = 1.0f;

	SGD::Point nextmove = this->GetPosition();

	if (input->IsKeyDown(SGD::Key::A) && GetPosition().x > 0.0f){
		nextmove.x -= m_moveSpeed;
		UpdateRects(nextmove);
		for (unsigned int x = 0; x < lay->GetCollisionPoints().size(); ++x){
			SGD::Rectangle tmp = SGD::Rectangle(lay->GetCollisionPoints().at(x).x - offset.x,
				lay->GetCollisionPoints().at(x).y - offset.y, 0, 0);

			if (tmp.left > -32 && tmp.left < 1024 - tmp.left + 1020 && tmp.top > -32 && tmp.top < 768 - tmp.top + 764){
				tmp.right = tmp.left + 32;
				tmp.bottom = tmp.top + 32;
				if (m_PlayerRect.IsIntersecting(tmp) == true){
					nextmove = GetPosition();
					nextmove.x += m_moveSpeed;
				}
			}
		}
	}
	else if (input->IsKeyDown(SGD::Key::D) && GetPosition().x + 32.0f < wsize.height){
		nextmove.x += m_moveSpeed;
		UpdateRects(nextmove);
		for (unsigned int x = 0; x < lay->GetCollisionPoints().size(); ++x){
			SGD::Rectangle tmp = SGD::Rectangle(lay->GetCollisionPoints().at(x).x - offset.x,
				lay->GetCollisionPoints().at(x).y - offset.y, 0, 0);

			if (tmp.left > -32 && tmp.left < 1024 - tmp.left + 1020 && tmp.top > -32 && tmp.top < 768 - tmp.top + 764){
				tmp.right = tmp.left + 32;
				tmp.bottom = tmp.top + 32;
				if (m_PlayerRect.IsIntersecting(tmp) == true){
					nextmove = GetPosition();
					nextmove.x -= m_moveSpeed;
				}
			}
		}
	}
	else if (input->IsKeyDown(SGD::Key::W) && GetPosition().y > 0.0f){
		nextmove.y -= m_moveSpeed;
		UpdateRects(nextmove);
		for (unsigned int x = 0; x < lay->GetCollisionPoints().size(); ++x){
			SGD::Rectangle tmp = SGD::Rectangle(lay->GetCollisionPoints().at(x).x - offset.x,
				lay->GetCollisionPoints().at(x).y - offset.y, 0, 0);

			if (tmp.left > -32 && tmp.left < 1024 - tmp.left + 1020 && tmp.top > -32 && tmp.top < 768 - tmp.top + 764){
				tmp.right = tmp.left + 32;
				tmp.bottom = tmp.top + 32;
				if (m_PlayerRect.IsIntersecting(tmp) == true){
					nextmove = GetPosition();
					nextmove.y += m_moveSpeed;
				}
			}
		}
	}
	else if (input->IsKeyDown(SGD::Key::S) && GetPosition().y + 32.0f < wsize.width){
		nextmove.y += m_moveSpeed;
		UpdateRects(nextmove);
		for (unsigned int x = 0; x < lay->GetCollisionPoints().size(); ++x){
			SGD::Rectangle tmp = SGD::Rectangle(lay->GetCollisionPoints().at(x).x - offset.x,
				lay->GetCollisionPoints().at(x).y - offset.y, 0, 0);

			if (tmp.left > -32 && tmp.left < 1024 - tmp.left + 1020 && tmp.top > -32 && tmp.top < 768 - tmp.top + 764){
				tmp.right = tmp.left + 32;
				tmp.bottom = tmp.top + 32;
				if (m_PlayerRect.IsIntersecting(tmp) == true){
					nextmove = GetPosition();
					nextmove.y -= m_moveSpeed;
				}
			}
		}
	}
	SetPosition(nextmove);
	UpdateRects(this->GetPosition());
}

void Player::Render(){
	SGD::GraphicsManager* graphics = SGD::GraphicsManager::GetInstance();

	graphics->DrawTexture(m_hImage,				//	Offset based on camera position
		SGD::Point(GetPosition().x - m_CamOffset.x, GetPosition().y - m_CamOffset.y),
		0.0f,
		{},
		SGD::Color(255, 255, 255, 255),
		SGD::Size(1.0f, 1.0f));

	/*if (!m_PlayerRect.IsEmpty() || !m_PlayerSmellRect.IsEmpty()){
		graphics->DrawRectangle(m_PlayerRect,
			SGD::Color(0, 0, 0, 0),
			SGD::Color(255, 255, 0, 0), 1);

		graphics->DrawRectangle(m_PlayerSmellRect,
			SGD::Color(0, 0, 0, 0),
			SGD::Color(255, 0, 0, 255), 1);
	}*/
}

void Player::HandleCollision(const IEntity* pOther){

}

int Player::GetType() const {
	return ENT_PLAYER;
}

SGD::Rectangle Player::GetRect() const {
	return m_PlayerRect;
}

void Player::UpdateRects(SGD::Point p){
	m_PlayerRect = SGD::Rectangle(p.x - m_CamOffset.x,
		p.y - m_CamOffset.y,
		(p.x + 32) - m_CamOffset.x,
		(p.y + 32) - m_CamOffset.y);

	m_PlayerSmellRect = SGD::Rectangle(p.x - m_CamOffset.x - m_SmellRadius,
		p.y - m_CamOffset.y - m_SmellRadius,
		(p.x + 32) - m_CamOffset.x + m_SmellRadius,
		(p.y + 32) - m_CamOffset.y + m_SmellRadius);
}

void Player::PreformBehavaior(){

}
